package com.ivan.toolkit.swing;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.text.JTextComponent;

public class TextComponentUtils {

	public TextComponentUtils() {
	}
	
	private static void errorForTextComonent(JTextComponent comp, String message) {
		throw new RuntimeException(message);
	}
	
	public static void verifyNotEmpty(JTextComponent comp, String message) {
		if(comp.getText().isEmpty()) 
			errorForTextComonent(comp, message);
	}
	
	public static void verifyEqual(JTextComponent comp1, JTextComponent comp2, String message) {
		if(!comp1.getText().equals(comp2.getText())) 
			errorForTextComonent(comp1, message);
	}
	
	public static void verifyMinLength(JTextComponent comp, int min, String message) {
		String text = comp.getText();
		if(text.length() < min)
			errorForTextComonent(comp, message);
	}
	
	public static void verifyMaxLength(JTextComponent comp, int max, String message) {
		String text = comp.getText();
		if(text.length() > max)
			errorForTextComonent(comp, message);
	}
	
	public static void verifyPattern(JTextComponent comp, String pattern, String message) {
		String text = comp.getText();
		try {
			Pattern p = Pattern.compile(pattern);
			Matcher m = p.matcher(text);
			if(!m.matches())
				errorForTextComonent(comp, message);
		} catch(Exception e) {
			errorForTextComonent(comp, message);
		}
	}
	
	public static void verifyMobile(JTextComponent comp, String message) {
		String text = comp.getText();
		Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
		Matcher m = p.matcher(text);
		if(!m.matches())
			errorForTextComonent(comp, message);
	}
	
	public static void verifyIPV4(JTextComponent comp, String message) {
		String text = comp.getText();
		if(!isIPV4(text))
		//if(!IPAddressUtil.isIPv4LiteralAddress(text))
			errorForTextComonent(comp, message);
	}
	
	public static String toText(JTextComponent comp) {
		return comp.getText();
	}
	
	public static long toLong(JTextComponent comp) {
		String text = comp.getText();
		return Long.parseLong(text);
	}
	
	public static int toInteger(JTextComponent comp) {
		String text = comp.getText();
		return Integer.parseInt(text);
	}
	
	public static int toIntegerHex(JTextComponent comp) {
		return toInteger(comp, 10);
	}
	
	public static int toInteger(JTextComponent comp, int radix) {
		String text = comp.getText();
		return Integer.parseInt(text, radix);
	}
	
	public static boolean isIPV4(String ip) {
        boolean b = false;
        if(ip.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")){  
            String s[] = ip.split("\\.");  
            if(Integer.parseInt(s[0])<255)  
                if(Integer.parseInt(s[1])<255)  
                    if(Integer.parseInt(s[2])<255)  
                        if(Integer.parseInt(s[3])<255)  
                            b = true;  
        }  
        return b;  

	}

}
